<%--
    Document   : loanRequest
    Created on : 04 14, 11, 15:27:20 PM
    Author     : MonicaSandoval
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib  prefix="s" uri="/struts-tags" %>
<%@taglib  prefix="sj" uri="/struts-jquery-tags" %>
<%@taglib  prefix="sjg" uri="/struts-jquery-grid-tags" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<div id="requested" style="width:100%;text-align:center">
    <s:url  id="remoteurl" action="getRequests">
    <s:url id="loanappurl" value="loanprofile-loanapp.jsp" />
        <s:param name="cat">${param.cat}</s:param>
        <c:if test="${param.branches != null}">
            <s:param name="branches">${param.branches}</s:param>
        </c:if>
    </s:url>
    <s:url  id="nremoteurl" action="step"/>
    
        <script type="text/javascript">
            var y = 0;
            
            $.subscribe("gridCompleteTopics",function() {
                var ids = document.getElementById("selected").value;
                jQuery.each(y,function(index, item) {
                    jQuery("#gridtable").jqGrid("setSelection", item);
                });
                jQuery.each(ids,function(index, item) {
                    var flag = false;
                    for(var prop in y){
                        if(item == y[prop]){
                            flag = true;
                        }
                    }
                    if(!flag){
                        jQuery("#gridtable").jqGrid("setSelection", item);
                    }                                   
                });
                y = document.getElementById("selected").value;
            });

            $.subscribe('getselectedids', function(event,data) {
                var s = $("#gridtable").jqGrid('getGridParam','selarrrow');
                document.getElementById("selected").value = s;
                check();
            });

            function check(){

                if(document.getElementById("selected").value.length != 0){
                    while(document.getElementById("createReq").hasAttribute("style")){
                        document.getElementById("createReq").removeAttribute("style");
                    }
                    document.getElementById("createReq").style.fontSize = "12px";
                }else{
                    document.getElementById("createReq").style.display = "none";
                }
            }


            function proceedButton(){
                document.genreq.submit();
            }

            function submitButton(){
                 document.go.submit();
            }

            function cancelButton(){
                $('#dialog').dialog('close');
            }
                
            
            function formatLink(cellvalue, options, rowObject) {
                return "<a href='<s:property value="loanappurl" />?id="+rowObject["loanverificationid"]+"' onClick='javascript: return openDialog(this)'>" + cellvalue + "</a>";
            }
            
            function openDialog(e) {
                window.open(e.href, "newWindow");
                return false;
            }
        </script>
        <c:if test="${sessionScope.branch.branchcode == 'HO'}">
        <div style="align: right">
            <s:action name="getBranches" executeResult="false" var="branch"/>
            <s:form name="go" action="loanrequest.jsp?cat=headoffice" method="post">
                <s:select label="Filter by Branch"
                                  headerKey="-1" headerValue="Get All Branches"
                                  listKey="branchid"
                                  listValue="branchname"
                                  size="1"
                                  list="#branch.brList"
                                  name="branches"
                                  cssStyle="align: right"
                                  />
            </s:form>
            <sj:a
                name="yes"
                id="yes"
                button="true"
                cssStyle="font-size:12px"
                onclick="submitButton()"
                >
               Select
            </sj:a>
            </div>
            
     </c:if>
    <sjg:grid
        id="gridtable"
        caption="Loan Requests"
        dataType="json"
        href="%{remoteurl}"
        pager="true"
        gridModel="gridModel"
        rowList="10,15,20"
        rowNum="15"
        rownumbers="false" 
        multiselect="true"
        navigator="false"
        shrinkToFit="false"
        viewrecords="true"
        onCompleteTopics="gridCompleteTopics"        
        onSelectRowTopics="getselectedids"
        autowidth="true"
    >

        <sjg:grid id="nsubgrid"
                  subGridUrl="%{nremoteurl}"
                  gridModel="gridModel"
                  shrinkToFit="false"
                  rowNum="-1"
                  footerrow="false"
                  userDataOnFooter="false"
        >
            <sjg:gridColumn name="loanverificationid"
                            index="loanverificationid"
                            title="Loan Verification No."
                            key="true"
                            hidedlg="true"
                            hidden="true"
            />
            <sjg:gridColumn name="documentno" 
                            index="docno"
                            title="Document No."
                            formatter = "formatLink"
                            search="true"
                            align="center"
                            cssClass="gridlink"
                            width=""
            />
            <sjg:gridColumn name="borrowername"
                            index="borrowername"
                            title="Borrower Name"
                            align="center"
            />
            <sjg:gridColumn name="amountrequested"
                            index="amountrequested"
                            title="Promissory Note"
                            formatter="currency"
                            align="right"
            />
            <sjg:gridColumn name="area" 
                            index="area"
                            title="Area"
                            align="center"
            />
            <sjg:gridColumn name="releasedate"
                            index="release"
                            title="Specific Date of Release"
                            align="center"
            />
            <sjg:gridColumn name="borroweraddress"
                            index="raddress"
                            title="Residence Address"
                            align="center"
           />
            <sjg:gridColumn name="businessaddresses"
                            index="baddress"
                            title="Business Address"
            />
            <sjg:gridColumn name="businesstypes" 
                            index="btypes"
                            title="Business Type"
                            align="center"
            />
            <sjg:gridColumn name="collaterals" 
                            index="collaterals"
                            title="Collaterals"
                            align="center"
            />
            <sjg:gridColumn name="firstlast" 
                            index="firstlast"
                            title="Mode of Payment (1st Last)"
                            align="center"
            />
            <sjg:gridColumn name="secondlast"
                            index="secondlast"
                            title="Mode of Payment (2nd Last)"
                            align="center"
            />
            <sjg:gridColumn name="firstlastpn"
                            index="firstlastpn"
                            title="PN Amount (1st Last)"
                            formatter="currency"
                            align="right"
            />
            <sjg:gridColumn name="secondlastpn" 
                            index="secondlastpn"
                            title="PN Amount (2nd Last)"
                            formatter="currency"
                            align="right" />
            <sjg:gridColumn name="auditor" 
                            index="empname"
                            title="Auditor Confirmation"
                            align="center"
            />
        </sjg:grid>

        <sjg:gridColumn name="requestid" index="requestid" title="Sequence No." formatter="integer" sortable="false" key="true"/>
        <sjg:gridColumn name="requestdate" index="date" title="Request Date" sortable="true" editable="false"/>
        <sjg:gridColumn name="employees.completename" index="emp" title="Verified By" sortable="true" editable="false"/>
        <sjg:gridColumn name="branchname" index="branch" title="Branch" sortable="true" editable="false" />
        
    </sjg:grid>
        <br />
        <hr/>
        <sj:dialog
            id="dialog"
            autoOpen="false"
            modal="true"
            buttons = "{
                    'Verify':function() { proceedButton(); },
                    'Cancel':function() { cancelButton(); }
                    }"
            title="Save Request"
        >
            Are you sure you want to verify selected request(s)?
            <s:form name="genreq" action="updaterequest" method="post" >
                <s:textfield name="selected" id="selected" value="" cssStyle="display:none"/>
            </s:form>
        </sj:dialog>
        <sj:a
            name="createReq"
            id="createReq"
            openDialog="dialog"
            button="true"
            buttonIcon="ui-icon-disk"
            cssStyle="display:none"
        >
            Verify Requests
        </sj:a>
</div>
